package com.controller;
import com.pojo.Tch;
import com.service.TchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/tch")
public class TchController {

    @Autowired
//    @Qualifier("StuServiceImpl")//自动装配把autowire = “byName”
    private TchService tchService;

    // 查询全部
    @RequestMapping("alltch")
    public String alltch(Model model) {
        List<Tch> list = tchService.queryAlltch();
        model.addAttribute("list", list);
        System.out.println("the: " + list);
        return "alltch";
    }
    // 删除
    @RequestMapping("/deltch/{tid}")
    public String deletetid(@PathVariable("tid") int tid) {
        tchService.deleteBytid(tid);
        return "redirect:/tch/alltch";
    }
    // 增加
    @RequestMapping("/toAddtch")
    public String toAddPaper() {
        return "addtch";
    }

    @RequestMapping("/addtch")
    public String addPaper(Tch tch) {
        System.out.println(tch);
        tchService.addtch(tch);
        return "redirect:/tch/alltch";
    }

    //修改
    @RequestMapping("/toUpdatetch")
    public String toUpdatetch(Model model, int tid) {
        Tch tch = tchService.queryBytid(tid);
        System.out.println(tch);
        model.addAttribute("tch",tch);
        return "updatetch";
    }
    @RequestMapping("/updatetch")
    public String updatetch(Model model, Tch tch) {
        System.out.println(" update: " + tch);
        tchService.updatetch(tch);
        tch = tchService.queryBytid(tch.getTid());
        System.out.println(" update: " + tch);
        return "redirect:/tch/alltch";
    }


}
